package com.april.product.controller;

import com.april.common.constant.MallUrl;
import com.april.common.utils.Result;
import com.april.product.entity.Category;
import com.april.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;



/**
 * 商品三级分类
 *
 * @author april
 * @email 2673888518@qq.com
 * @date 2023-07-13 00:31:15
 */
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping(MallUrl.product.PRODUCT_CATEGORY_TREE)
    public Result list(){
        return Result.putResult(categoryService.listWithTree());
        // return Result.ok().put("data", categoryService.listWithTree());
    }


    /**
     * 信息
     */
    @RequestMapping(MallUrl.product.PRODUCT_CATEGORY_INFO)
    public Result info(@PathVariable("catId") Long catId){
        Category category = categoryService.getById(catId);
        return Result.putResult(category);
    }

    /**
     * 保存
     */
    @PostMapping(MallUrl.product.PRODUCT_CATEGORY_ADD)
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping(MallUrl.product.PRODUCT_CATEGORY_UPDATE)
    public Result update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok();
    }

    /**
     * 批量修改菜单顺序
     */
    @PostMapping(MallUrl.product.PRODUCT_CATEGORY_SORT)
    public Result updateSort(@RequestBody Category[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping(MallUrl.product.PRODUCT_CATEGORY_DELETE)
    public Result delete(@RequestBody Long[] catIds){
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return Result.ok();
    }

}
